﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XsClient.Common;

namespace XsClient.Controls
{
    /// <summary>
    /// UCBookItem.xaml 的交互逻辑
    /// </summary>
    public partial class UCBookItem : UserControl
    {
        public event EventHandler Click;
        /// <summary>
        /// 右键菜单事件
        /// </summary>
        public event Action<UCBookItem,int> RightMIClick;
        private bool itemSelected = false;
        private Brush defBrush = new SolidColorBrush(Color.FromRgb(0x1E, 0x1E, 0x1E));// #1e1e1e)
        private Brush selBrush = new SolidColorBrush(Color.FromRgb(0x3E, 0x3E, 0x3E));
        /// <summary>
        /// 设置选中
        /// </summary>
        public bool ItemSelected
        {
            get { return itemSelected; }
            set
            {
                itemSelected = value;
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.Background = value ? selBrush : defBrush;
                }));
            }
        }

        public UCBookItem()
        {
            InitializeComponent();
        }
        private BookInfo _Info;
        /// <summary>
        /// 数据
        /// </summary>
        public BookInfo Data
        {
            get { return _Info; }
            set
            {
                _Info = value;
                if (value != null)
                {
                    lbName.Content = value.Name;
                    lbAnchor.Content = value.Author;
                    lbNewChaper.Content = value.NowChapterName;
                }
            }
        }

        private void LbName_MouseDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void lbName_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            if (Click != null)
            {
                Click(this, e);
                ItemSelected = true;
            }
        }


        private void MenuItemUp_Click(object sender, RoutedEventArgs e)
        {
            if (RightMIClick != null)
            {
                RightMIClick(this, 1);
            }
        }

        private void MenuItemRemove_Click(object sender, RoutedEventArgs e)
        {

            if (RightMIClick != null)
            {
                RightMIClick(this, 0);
            }
        }

        private void MenuItemDown_Click(object sender, RoutedEventArgs e)
        {

            if (RightMIClick != null)
            {
                RightMIClick(this, -1);
            }
        }

        private void lbName_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            if (Click != null)
            {
                Click(this, e);
                ItemSelected = true;
            }
        }

        private void MenuItemUp2Top_Click(object sender, RoutedEventArgs e)
        {

            if (RightMIClick != null)
            {
                RightMIClick(this, 99);
            }
        }

        private void MenuItemDown2End_Click(object sender, RoutedEventArgs e)
        {
            if (RightMIClick != null)
            {
                RightMIClick(this, -99);
            }
        }
    }
}
